<!-- Dom模板 -->
<template>
  <div class="login">
    <!-- Dom内容 -->
    <div class="loginbox">
      <div class="login">
        <div class="main">
          <div class="form">
            <el-form
              :model="ruleForm"
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
              @keyup.enter.native="submitForm('ruleForm')"
            >
              <el-form-item label="账号" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')"
                  >登录</el-button
                >
                <el-button @click="resetForm('ruleForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import a from './a'; // 引入组件
import * as apis from '../api/user'
export default {
  name: "",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 5, message: "长度5到6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度5到6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  // Vue方法定义
  methods: {
    submitForm(formName) {
      apis.login({
		  username:this.ruleForm.username,
		  password:this.ruleForm.password
	  }).then((res) => {
		  console.log(res.data)
        if (res.data.code === "01") {
          this.$message({
            message: res.data.msg,
            type: "sucess",
          });
          localStorage.setItem(
            "user",
            JSON.stringify(res.data.Info)
          );
          this.$router.push("/home");
        } else if (res.data.code === "02") {
          this.$message({
            message: res.data.msg,
            type: "sucess",
          });
        } else if (res.data.code === "03") {
          this.$message({
            message: res.data.msg,
            type: "sucess",
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
/* @import url(''); 引入css类 */
.login {
  width: 100%;
  height: 100vh;
  background: url("../../public/images/pic.jpg") no-repeat center;
  background-size: cover;
}

.main {
  display: flex;
  justify-content: center;
  top: 300px;
  position: relative;
  height: 400px;
}

.form {
  width: 400px;
}
</style>
